<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\tab\util\titem;

use builder\base\InvalidInstanceException;
use builder\base\ViewBuilderInterface;

/**
 * 构建组件项
 * @author cleverstone
 * @since ym2.0
 */
class Builder extends TabItemType
{
    /**
     * @return string
     */
    protected function name()
    {
        return 'tab_item_builder';
    }

    /**
     * ym2构建器, 在JS对象window.ym2BehaviorReqExport中已内置重置属性
     * @return $this
     */
    public function switchReload()
    {
        $this->reloadProperty = true;
        return $this;
    }

    /**
     * @return array
     * @throws InvalidInstanceException
     */
    public function _toArray()
    {
        if (!$this->content instanceof ViewBuilderInterface) {
            throw new InvalidInstanceException('Content item must be instance of "builder\base\ViewBuilderInterface".');
        }

        if (!$this->content instanceof \builder\base\Builder) {
            throw new InvalidInstanceException('Content item must be instance of "builder\base\Builder".');
        }

        return [
            'id' => $this->id,
            'content' => $this->content->render(true),
            'selected' => $this->selected,
            'reloadProperty' => $this->reloadProperty ? $this->content->id : '',
        ];
    }
}